package se.hiq.demo.client;

import se.hiq.demo.client.event.AddOrderEvent;
import se.hiq.demo.client.event.AddOrderEventHandler;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class MVPEventBus implements EntryPoint {
	
	private final HandlerManager eventBus = new HandlerManager(null);
	
	final Button button = new Button();
	
	final FlexTable flexTable = new FlexTable();
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		bind(); 
		
		button.setText("Add Order");
		
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new AddOrderEvent());
			}
		});
		
		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		RootPanel.get("button").add(button);
		
		flexTable.addStyleName("cw-FlexTable");
	    flexTable.setWidth("32em");
	    flexTable.setCellSpacing(5);
	    flexTable.setCellPadding(3);

		RootPanel.get("list").add(flexTable);

		// Focus the cursor on the name field when the app loads
		button.setFocus(true);
	}
	
	private void bind(){
		eventBus.addHandler(AddOrderEvent.TYPE, new AddOrderEventHandler() {
			public void onAddOrder(AddOrderEvent event) {
				doAddOrder();
			}
		});
	}

	protected void doAddOrder() {
		int numRows = flexTable.getRowCount();
	    flexTable.setWidget(numRows, 0, new Label("Order #" + numRows));
		
	}
}
